<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *供货商对账
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['isonemerchid']=array('总部收款','商户收款');
$tabs=array(
	array('op'=>'list','title'=>'收款记录'),
	array('op'=>'listday','title'=>'日收款统计'),
	array('op'=>'listmonth','title'=>'月收款统计'),
);
$install = check_account_permission('jy_merch');
if(!$install){
	exi('请联系管理员开启<多商户>！','error');
}
//供货商对账
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'ispay'=>1,'pay_id >'=>0);
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(!empty($_RQ['merchid'])){
		$params['merchid']=trim($_RQ['merchid']);
		$return['params']['merchid']=$_RQ['merchid'];
	}else{
		$params['merchid >']=0;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','ordersn','status','createtime','ispay','price','merchid','pay_id');
	$list=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$merch=pdo_get('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['merchid']));
			$item['merchtitle']=$merch['merchname']?:'无';
			$item['merchimg']=$merch['logo'];
			$item['onemerchid']=pdo_getcolumn('core_paylog',array('id'=>$item['pay_id']),'onemerchid');
			$item['onemerchid']=$item['onemerchid']>0?1:0;
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'这里只显示，在平台进行支付的订单（商户线下支付的订单不显示）','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'单号','type'=>'string'),
			array('name'=>'mid','title'=>'用户ID','type'=>'string'),
			array('name'=>'member','title'=>'用户','type'=>'member'),
			array('type'=>'img_string','title'=>'商户信息','img'=>'merchimg',
			'data'=>array(
				array('title'=>'ID：','name'=>'merchid'),
				array('title'=>'名称：','name'=>'merchtitle')
				)
			),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'onemerchid','title'=>'收款类型','type'=>'radio','options'=>'isonemerchid'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'order_status'),
		)),
		'search'=>array(
			array('name'=>'ordersn','width'=>15,'title'=>'单号','type'=>'string'),
			array('name'=>'merchid','width'=>15,'title'=>'所属商户','type'=>'choose','action'=>'jy_merch/get_merch/'),
		)
	);
	$return['order_status']=$this->order_status;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='listday'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['merchname'])){
		$params['merchname like']='%'.trim($_RQ['merchname']).'%';
		$return['params']['merchname']=$_RQ['merchname'];
	}
	if(!empty($_RQ['realname'])){
		$params['realname like']='%'.trim($_RQ['realname']).'%';
		$return['params']['realname']=$_RQ['realname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=intval($_RQ['mobile']);
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['status']!=''){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$fields=array('id','mid','star','isrecommand','salecate','cateid','mobile','realname','status','jointime','accounttime','logo','merchname');
    $orderby='star desc, id desc';
	$total='';
	$list=pdo_getslice('jy_merch_user',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$day=$_RQ['day']?strtotime($_RQ['day']):mktime(0,0,0,date('m'),date('d'),date('Y'));
	$return['params']['day']=date('Y-m-d',$day);
	$starttime=$day;
	$endtime=$starttime+24*3600;
	$params=array('ispay'=>1,'uniacid'=>$_SESSION['uniacid']);
	foreach($list as &$v){
		$v['day']=$return['params']['day'];
		$v['catename']=pdo_getcolumn('jy_merch_category',array('id'=>$v['cateid'],'uniacid'=>$_SESSION['uniacid']),'catename');
		$params['createtime between']=array($starttime,$endtime);
		$v['fee']=pdo_getsum('jy_weishop_order',$params,'price');
		$v['fee']=$v['fee']?:0;
		$params['merchid']=intval($v['id']);
		$v['merchfee']=pdo_getsum('jy_weishop_order',$params,'price');
		$v['merchfee']=$v['merchfee']?:0;
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
				array('name'=>'day','title'=>'日期','type'=>'string'),
				array('title'=>'商户信息_Merch','type'=>'img_string','img'=>'logo',
				'data'=>array(
					array('title'=>'名称：','name'=>'merchname'),
					array('title'=>'项目：','name'=>'salecate')
					)
				),
				array('name'=>'catename','title'=>'所属分类','type'=>'string'),
				array('name'=>'fee','title'=>'总部业绩','type'=>'unitstring','unit'=>'元'),
				array('name'=>'merchfee','title'=>'商户业绩','type'=>'unitstring','unit'=>'元'),
				array('name'=>'jointime','title'=>'入驻时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'merchname','width'=>15,'title'=>'商户名称','type'=>'string'),
			array('name'=>'day','width'=>10,'title'=>'日期','type'=>'time'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$return['months']=array(
		'1'=>'1月',
		'2'=>'2月',
		'3'=>'3月',
		'4'=>'4月',
		'5'=>'5月',
		'6'=>'6月',
		'7'=>'7月',
		'8'=>'8月',
		'9'=>'9月',
		'10'=>'10月',
		'11'=>'11月',
		'12'=>'12月',
	);
	exi($return);
}
if($_RQ['op']=='listmonth'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(!empty($_RQ['merchname'])){
		$params['merchname like']='%'.trim($_RQ['merchname']).'%';
		$return['params']['merchname']=$_RQ['merchname'];
	}
	if(!empty($_RQ['realname'])){
		$params['realname like']='%'.trim($_RQ['realname']).'%';
		$return['params']['realname']=$_RQ['realname'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=intval($_RQ['mobile']);
		$return['params']['mobile']=$_RQ['mobile'];
	}
	if($_RQ['status']!=''){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$fields=array('id','mid','star','isrecommand','salecate','cateid','mobile','realname','status','jointime','accounttime','logo','merchname');
    $orderby='star desc, id desc';
	$total='';
	$return['list']=pdo_getslice('jy_merch_user',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$y=$_RQ['y']?:date('Y');
	$m=$_RQ['m']?:date('m');
	$return['params']['y']=$y;
	$return['params']['m']=$m;
	$starttime=strtotime($y.'-'.$m);
	$endtime=strtotime($y.'-'.$m+1);
	$params=array('ispay'=>1,'uniacid'=>$_SESSION['uniacid']);
	foreach($return['list'] as &$v){
		$v['month']=$y.'-'.$m;
		$v['catename']=pdo_getcolumn('jy_merch_category',array('id'=>$v['cateid'],'uniacid'=>$_SESSION['uniacid']),'catename');
		$params['createtime between']=array($starttime,$endtime);
		$v['fee']=pdo_getsum('jy_weishop_order',$params,'price');
		$v['fee']=$v['fee']?:0;
		$params['merchid']=intval($v['id']);
		$v['merchfee']=pdo_getsum('jy_weishop_order',$params,'price');
		$v['merchfee']=$v['merchfee']?:0;
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
				array('name'=>'month','title'=>'月份','type'=>'string'),
				array('title'=>'商户信息_Merch','type'=>'img_string','img'=>'logo',
				'data'=>array(
					array('title'=>'名称：','name'=>'merchname'),
					array('title'=>'项目：','name'=>'salecate')
					)
				),
				array('name'=>'catename','title'=>'所属分类','type'=>'string'),
				array('name'=>'fee','title'=>'总部业绩','type'=>'unitstring','unit'=>'元'),
				array('name'=>'merchfee','title'=>'商户业绩','type'=>'unitstring','unit'=>'元'),
				array('name'=>'jointime','title'=>'入驻时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'merchname','width'=>15,'title'=>'商户名称','type'=>'string'),
			array('name'=>'y','width'=>15,'title'=>'年份','type'=>'radio','options'=>'ys'),
			array('name'=>'m','width'=>15,'title'=>'月份','type'=>'radio','options'=>'months'),
		)
	);
	$return['htmls']=$htmls;
	$nowy=date('Y');
	$y1=$nowy-2;
	$y2=$nowy-1;
	$return['ys']=array(
		$y1=>$y1.'年',
		$y2=>$y2.'年',
		$nowy=>$nowy.'年',
	);
	$return['months']=array(
		'01'=>'1月',
		'02'=>'2月',
		'03'=>'3月',
		'04'=>'4月',
		'05'=>'5月',
		'06'=>'6月',
		'07'=>'7月',
		'08'=>'8月',
		'09'=>'9月',
		'10'=>'10月',
		'11'=>'11月',
		'12'=>'12月',
	);
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}